﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AD.Contract;
using AD.IBLL;
using AD.Contract.Models;
using AD.DAL;
using HZT.Framework.Contract;
using HZT.Framework.Utility;
using System.Data.Entity.Validation;
using System.Diagnostics;

namespace AD.BLL
{
    public class ADService : IADService
    {
        //获取部门列表
        public IEnumerable<Department> GetDepartmentList()
        {
            using (var dbContext = new ADDbContext())
            {
                IQueryable<Department> depts = dbContext.Departments;

                return depts.ToList();
            }
        }
        //获取客户列表
        public IEnumerable<Customer> GetCustomerList(CommonRequest request)
        {
            using (var dbContext = new ADDbContext())
            {
                IQueryable<Customer> customers = dbContext.Customers;
                if ((request != null) && !string.IsNullOrEmpty(request.Name))
                {
                    customers = customers.Where(u => u.Name.Contains(request.Name));
                }
                return customers.OrderBy(u => u.nID).ToPagedList(request.PageIndex, request.PageSize);
            }
        }
        //获取播出时段
        public IEnumerable<Period> GetPeriodList(CommonRequest request)
        {
            using (var dbContext = new ADDbContext())
            {
                IQueryable<Period> reqList = dbContext.Periods;
                if ((request != null) && !string.IsNullOrEmpty(request.Name))
                {
                    reqList = reqList.Where(u => u.Name.Contains(request.Name));
                }
                return reqList.OrderBy(u => u.nID).ToPagedList(request.PageIndex, request.PageSize);
            }
        }

        //获取广告业务分类，根据类型不同
        public IEnumerable<BaseCategory> GetBaseCategory(string categoryType)
        {
            using (var dbContext = new ADDbContext())
            {
                IQueryable<BaseCategory> categorys = dbContext.BaseCategories.Where(p => p.CategoryType == categoryType);

                return categorys.ToList();
            }
        }
        //广告列表
        public IEnumerable<ADBill> GetADBill(ADBillRequest request = null)
        {
            request = request ?? new ADBillRequest();
            using (var dbContext = new ADDbContext())
            {
                IQueryable<ADBill> queryList = dbContext.ADBills.Include("dept").Include("customer");
                //var model = request.ADBill;
                if(!string.IsNullOrEmpty(request.ADBill.Status))
                    queryList = queryList.Where(m => m.Status == request.ADBill.Status);

                if (!string.IsNullOrEmpty(request.ADBill.DepartmentId))
                    queryList = queryList.Where(m => m.DepartmentId == request.ADBill.DepartmentId);

                if (!string.IsNullOrEmpty(request.ADBill.CustomerId))
                    queryList = queryList.Where((m => m.ContractId.Contains(request.ADBill.CustomerId)));

                return queryList.OrderByDescending(u => u.Id).ToPagedList(request.PageIndex, request.PageSize);
            }
        }
        //
        public ADBill GetADBillById(string Id)
        {
            using (var dbContext = new ADDbContext())
            {
                return dbContext.ADBills.Where(u => u.Id == Id).SingleOrDefault();
            }
        }
        //保存广告
        public void SaveADBill(ADBill model)
        {
            try
            {
                using (var dbContext = new ADDbContext())
                {
                    if (!string.IsNullOrEmpty(model.Id))
                    {
                        dbContext.Update<ADBill>(model);
                        dbContext.SaveChanges();
                    }
                    else
                    {
                        // var existUser = dbContext.FindAll<Department>(u => u.LoginName == dept.LoginName);
                        model.Id = CommonFunction.GetNewID();
                        dbContext.Insert<ADBill>(model);
                        dbContext.SaveChanges();
                    }
                }
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                    }
                }
            }
        }


        public void SaveADCharge(ADCharge model)
        {
            using (var dbContext = new ADDbContext())
            {
                if (!string.IsNullOrEmpty(model.Id))
                {
                    dbContext.Update<ADCharge>(model);
                    dbContext.SaveChanges();
                }
                else
                {
                    // var existUser = dbContext.FindAll<Department>(u => u.LoginName == dept.LoginName);
                    model.Id = CommonFunction.GetNewID();
                    dbContext.Insert<ADCharge>(model);
                    dbContext.SaveChanges();
                }
            }
        }
        public void SaveADAppendix(ADAppendix model)
        {
            using (var dbContext = new ADDbContext())
            {
                if (!string.IsNullOrEmpty(model.Id))
                {
                    dbContext.Update<ADAppendix>(model);
                    dbContext.SaveChanges();
                }
                else
                { 
                    model.Id = CommonFunction.GetNewID();
                    dbContext.Insert<ADAppendix>(model);
                    dbContext.SaveChanges();
                }
            }
        }
    }
}
